﻿Public Class login1
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		Me.txtUserId.Attributes.Add("onBlur", "ChangFormatID(this);")
		Me.txtUserId.Attributes.Add("onFocus", "ChangFormatID_OnFocus(this);")
    End Sub

	Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnLogin.Click
		Dim userinfo As Entity.UserInfoItem
		Dim remember As Boolean = False	'จำการ login
		Dim role As String

		Try

			userinfo = BL.Officer.Authen(Me.txtUserId.Text.Replace("-", ""), Me.txtPassword.Text)

			BL.Authentication.LogInLog(HttpContext.Current.Request.UserHostAddress, userinfo.user_id)

			GlobalApp.Authen = userinfo

			If userinfo.role_id = 1 Then
				role = "member"
			Else
				role = "admin"
			End If

			Dim ticket As New FormsAuthenticationTicket(1, userinfo.user_id, DateTime.Now, DateTime.Now.AddMinutes(SS.ApplicationBlock.Config.GetWebConfig("LiftTimeAuthen")), remember, role, FormsAuthentication.FormsCookiePath)
			Dim hashCookies As String = FormsAuthentication.Encrypt(ticket)
			Dim cookie As New HttpCookie(FormsAuthentication.FormsCookieName, hashCookies)
			Response.Cookies.Add(cookie)

			If userinfo.user_force_change.Equals("Y") Then
				Response.Redirect("changepwd.aspx", False)
			Else
				Response.Redirect("mainpage.aspx", False)
			End If
		Catch ex As Exception
			Master.Alert(ex.Message)
		End Try
	End Sub
End Class